from pyecharts.charts import Bar, Line, Grid, WordCloud
from bilibili_danmu_analysis.analysis.analysis_dao import AnalysisDao
import os



class VisualDao:
    def __init__(self,download_path):
        self.analysis=AnalysisDao()
        self.download_path=download_path
    def genarate_line(self, bvid):
        try:
            msg=self.analysis.get_bvid_timestamp(bvid)
            if msg["code"]!=200:
                return msg
            time_dict=msg["data"]
            print(time_dict.keys(), time_dict.values())
            line=Line()
            line.add_xaxis(list(time_dict.keys()))
            line.add_yaxis("弹幕数量", list(time_dict.values()))
            line.render(self.download_path + f"/{bvid}_line.html")
            return {"code": 200, "msg": "success"}
        except Exception as e:
            return {"code": 500, "msg": str(e)}

    def genarate_word_cloud(self, bvid):
        try:
            msg = self.analysis.statisic_danmu_word(bvid)
            if msg["code"] != 200:
                return msg
            word_cloud = WordCloud()
            word_cloud.add("", msg["data"].items(), word_size_range=[20, 100])  # 添加数据
            word_cloud.render(self.download_path + f"/{bvid}_word_cloud.html")
            return {"code": 200, "msg": "success"}
        except Exception as e:
            return {"code": 500, "msg": str(e)}



if __name__ == "__main__":
    download_path = os.path.join(os.path.dirname(__file__), "..", "download")
    visual = VisualDao(download_path)
    print(visual.genarate_line("BV11NoCYmEkP"))
